//存token
export const SETTOKEN = (token: string, expires_in?: number) => {
    setItem("TOKEN", token, expires_in || 3600 * 24 * 7)
}
export const GETTOKEN = () => {
    return getItem("TOKEN")
}

export const SET_REFRESH_TOKEN = (token: string) => {
    setItem("REFRESH_TOKEN", token)
}
export const GET_REFRESH_TOKEN = () => {
    return getItem("REFRESH_TOKEN")
}

export const GETUSERINFO = () => {
    return getItem("USERINFO") || {}
}

export const SETUSERINFO = (info: any, expires_in?: number) => {
    const data = {} as any;
    if (info.UserID && info.Mobile?.indexOf('-') > -1) {
        const [regionCode, phone] = info.Mobile.split('-')
        data.mobile = info.Mobile;
        data.regionCode = Number(regionCode);
        data.phone = phone;
        data.userId = info.UserID
    } else {
        Object.assign(data, info);
    }
    setItem("USERINFO", data, expires_in)
}

export const REMOVETOKEN = () => {
    setItem("REFRESH_TOKEN", null);
    setItem("TOKEN", null)
}
// //暗黑主题
// export const SETDARK = (flag: boolean) => {
//     localStorage.setItem("DARK", JSON.stringify(flag))
// }
// export const GETDARK = () => {
//     return JSON.parse(localStorage.getItem("DARK") as string)
// }
// //颜色主题
// export const SETCOLOR = (color: string) => {
//     localStorage.setItem("COLOR", color)
// }
// export const GETCOLOR = () => {
//     return localStorage.getItem("COLOR")
// }

const expire_key = "__expire_key_"
const prefix_key = "TRAFFIC_DOG_"

function convertValue(value: any) {
    if (typeof value === "string") {
        if (value.length === 1) {
            return value // 单个字符
        } else if (!isNaN(Number(value))) {
            return Number(value) // 数字字符串
        } else {
            try {
                return JSON.parse(value) // 尝试解析为 JSON 对象或数组
            } catch (e) {
                return value // 普通字符串
            }
        }
    } else if (typeof value === "number") {
        return value // 数字
    } else if (typeof value === "boolean") {
        return value // 布尔值
    } else if (Array.isArray(value)) {
        return value // 数组
    } else if (typeof value === "object" && value !== null) {
        return value // 对象
    } else {
        return null
    }
}

const getItem = (key: string, defaultValue?: any) => {
    const expire_time = Number(localStorage.getItem(expire_key + prefix_key + key) || 0)
    if (expire_time == 0 || expire_time * 1000 > Date.now()) {
        return convertValue(localStorage.getItem(prefix_key + key)) || defaultValue
    }
    return defaultValue || null
}

const setItem = (key: string, val: any = null, expire?: number) => {
    if (expire == 0 || val == null) {
        localStorage.removeItem(expire_key + prefix_key + key)
        localStorage.removeItem(prefix_key + key)
        return
    }
    if (typeof expire === "number") {
        if (expire < Date.now()) {
            //说明传的是秒数
            expire = expire * 1000 + Date.now()
        }
        localStorage.setItem(expire_key + prefix_key + key, Math.round(expire / 1000).toString())
    }
    localStorage.setItem(prefix_key + key, JSON.stringify(val))
}

//清除失效的缓存
for (let i = 0; i < localStorage.length; i++) {
    const key = localStorage.key(i)
    if (key?.indexOf(expire_key + prefix_key) === -1) {
        const real_key = key.replace(prefix_key, '')
        const value = localStorage.getItem(real_key)
        if (value && !getItem(real_key)) {
            setItem(real_key, null)
        }
    }
}

export default {
    getItem,
    setItem,
    convertValue,
}
